<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>{{ isEdit ? '修改物流信息' : '新增物流信息' }}</span>
      </div>
      
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
          <el-form-item label="订单号" prop="orderNo">
            <el-input v-model="form.orderNo" placeholder="请输入订单号" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
          <el-form-item label="物流公司" prop="logisticsCompany">
            <el-input v-model="form.logisticsCompany" placeholder="请输入物流公司" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
          <el-form-item label="物流单号" prop="logisticsNo">
            <el-input v-model="form.logisticsNo" placeholder="请输入物流单号" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
          <el-form-item label="物流状态" prop="logisticsStatus">
            <el-radio-group v-model="form.logisticsStatus">
              <el-radio
                v-for="dict in dict.type.ec_logistics_status"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
          <el-form-item label="发件人姓名" prop="senderName">
            <el-input v-model="form.senderName" placeholder="请输入发件人姓名" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
          <el-form-item label="发件人电话" prop="senderPhone">
            <el-input v-model="form.senderPhone" placeholder="请输入发件人电话" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="发件人地址" prop="senderAddress">
            <el-input v-model="form.senderAddress" type="textarea" placeholder="请输入发件人地址" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
          <el-form-item label="收件人姓名" prop="receiverName">
            <el-input v-model="form.receiverName" placeholder="请输入收件人姓名" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
          <el-form-item label="收件人电话" prop="receiverPhone">
            <el-input v-model="form.receiverPhone" placeholder="请输入收件人电话" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="收件人地址" prop="receiverAddress">
            <el-input v-model="form.receiverAddress" type="textarea" placeholder="请输入收件人地址" />
          </el-form-item>
        </el-col>
      </el-row>
        
        <el-form-item style="text-align: center; margin-top: 30px;">
          <el-button type="primary" @click="submitForm" :loading="loading">{{ isEdit ? '修改' : '新增' }}</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getLogistics, addLogistics, updateLogistics } from "@/api/fun/logistics"

export default {
  name: "LogisticsForm",
  dicts: ['ec_logistics_status'],
  data() {
    return {
      // 提交状态
      loading: false,
      // 是否为编辑模式
      isEdit: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    const logisticsId = this.$route.params && this.$route.params.logisticsId
    if (logisticsId) {
      this.isEdit = true
      this.getLogisticsInfo(logisticsId)
    }
  },
  methods: {
    /** 获取物流信息详情 */
    getLogisticsInfo(logisticsId) {
      getLogistics(logisticsId).then(response => {
        this.form = response.data
      })
    },
    // 表单重置
    reset() {
      this.form = {
        logisticsId: null,
        orderNo: null,
        logisticsCompany: null,
        logisticsNo: null,
        logisticsStatus: null,
        senderName: null,
        senderPhone: null,
        senderAddress: null,
        receiverName: null,
        receiverPhone: null,
        receiverAddress: null,
        createTime: null,
        updateTime: null
      }
      this.$nextTick(() => {
        if (this.$refs.form) {
          this.$refs.form.clearValidate()
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          if (this.form.logisticsId != null) {
            updateLogistics(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              // 通知父页面刷新列表
                this.$eventBus.$emit('refreshLogisticsList')
                this.$tab.closePage()
            }).finally(() => {
              this.loading = false
            })
          } else {
            addLogistics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              // 通知父页面刷新列表
              this.$eventBus.$emit('refreshLogisticsList')
              this.$tab.closePage()
            }).finally(() => {
              this.loading = false
            })
          }
        }
      })
    },
    /** 取消按钮 */
    cancel() {
      this.$tab.closePage()
    }
  }
}
</script>

<style scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}
</style>